package com.tyh.management.controller.dealcontroller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.tyh.management.entity.Admin;
import com.tyh.management.entity.Teacher;
import com.tyh.management.service.AdminService;
import com.tyh.management.service.StuService;
import com.tyh.management.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.tyh.management.entity.Stu;

@Controller
@RequestMapping("/Sys")
public class LoginResgisterDeal {

    @Autowired
    AdminService adminService;

    @Autowired
    StuService stuService;

    @Autowired
    TeacherService teacherService;


    @ResponseBody
    @RequestMapping(value = "/dealLogin")
    public String getInfo(@RequestParam(value = "num") String num, @RequestParam(value = "psw") String psw,
                          @RequestParam(value = "identify") String identify, HttpSession httpSession) {

        String dataJson = "fail";
        if (Integer.parseInt(identify) == 0) {
            List<Admin> adminList = new ArrayList<>();
            adminList = adminService.findAdmin(num, psw);
            if (adminList.size() > 0) {
                String account = adminList.get(0).getAccount();
                String name = adminList.get(0).getName();
                httpSession.setAttribute("account", account);
                httpSession.setAttribute("name", name);
                httpSession.setAttribute("photo", "admin.png");
                httpSession.setAttribute("role", "admin");
                dataJson = JSON.toJSONString(adminList);
                return dataJson;
            }
        } else if (Integer.parseInt(identify) == 2) {
            List<Stu> stuList = new ArrayList<>();
            stuList = stuService.findStu(num, psw);
            if (stuList.size() > 0) {
                String name = stuList.get(0).getName();
//                String photo = stuList.get(0).getPhoto();
                String account = stuList.get(0).getStuno();
                httpSession.setAttribute("account", account);
                httpSession.setAttribute("name", name);
//                httpSession.setAttribute("photo", photo);
                httpSession.setAttribute("role", "stu");
                dataJson = JSON.toJSONString(stuList);
                return dataJson;
            }
        }
        return "fail";
    }


    @RequestMapping(value = "/registerStuDeal")
    @ResponseBody
    public String registerDeal(@RequestBody Map map) {
        System.out.println("stu psw:" + map.get("psw"));
        map.put("psw", map.get("psw"));
        if (stuService.addStu(map) > 0) {
            return "success";
        }

        return "failure";
    }


    @RequestMapping(value = "/registerTeaDeal")
    @ResponseBody
    public String registerTeaDeal(@RequestBody Map map) {
        System.out.println("teacher psw:" + map.get("psw"));
        map.put("psw", map.get("psw").toString());

        if (teacherService.addTeacher(map) > 0) {
            return "success";
        }

        return "failure";
    }

}
